﻿using Asion.Dapper;
using Asion.Dapper.NetCore.Extend;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Reflection;
using System.Text;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class DatabaseServiceCollectionExtensions
    {

        /// <summary>
        ///  add db
        /// <para>load appsetting.json ConnectionStrings:DefaultConnection,ConnectionStrings:DefaultConnectionType if configure is null</para>
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configure"></param>
        /// <returns></returns>
        public static IServiceCollection AddDatabaseAccessor(this IServiceCollection services, Action<IServiceCollection> configure = null)
        {

            // add repository
            services.AddSingleton<IDbConnectionFactory, DbConnectionFactory>();

            if (configure == null)
            {
                AddDbs(services);
            }
            else
            {
                configure?.Invoke(services);
            }


            services.AddScoped(typeof(ICrudRepository<>), typeof(SimpleCrudRepository<>));
            //services.AddScoped(typeof(ICrudAsyncRepository<>), typeof(SimpleCrudAsyncRepository<>));

            services.AddScoped(typeof(IMultipleCrudRepository<>), typeof(SimpleMultipleCrudRepository<>));


            SqlMapperExtensions.PropertiesInitialize();
            return services;
        }


        /// <summary>
        /// add default db  by appsetting.json ConnectionStrings:DefaultConnection,ConnectionStrings:DefaultConnectionType
        /// </summary>
        /// <param name="services"></param>
        /// <exception cref="SettingsPropertyNotFoundException"></exception>
        public static void AddDefaultDbs(IServiceCollection services)
        {
            using (ServiceProvider sp = services.BuildServiceProvider())
            {
                var factory = sp.GetRequiredService<IDbConnectionFactory>();
                var configuration = sp.GetRequiredService<IConfiguration>();
                var defalutconnetionString = configuration["ConnectionStrings:DefaultConnection"];
                if (string.IsNullOrWhiteSpace(defalutconnetionString))
                {
                    throw new SettingsPropertyNotFoundException("connectionstring not setting");
                }
                var defaultConnectionType = configuration["ConnectionStrings:DefaultConnectionType"];
                var config = DbConfiguration.GetDbConfigurationByConnectionString(defalutconnetionString, defaultConnectionType);
                factory.Initialize(config);
            }
        }


        public static void AddDbs(IServiceCollection services)
        {
            if (services == null) throw new ArgumentNullException(nameof(services));
     
        }




    }
}
